<?php


namespace app\admin\model;


use app\common\lib\RespCode;
use app\common\model\BaseModel;
use custom\Err;
use oss\Oss;

class Adver extends BaseModel {

    /**
     * 列表
     * @param $params
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getList($params) {
        $count = self::count ();
        if (empty($count)) {
            $result = [
                'page'    => 1,
                'pageNum' => 1,
                'count'   => 0,
                'data'    => [],
            ];
        } else {
            $page = empty($params['page']) ? 1 : intval ($params['page']);
            $long = empty($params['long']) ? 10 : intval ($params['long']);
            $data = self::page ($page, $long)
                ->select ()
                ->toArray ();
            foreach ($data as $k => $v) {
                $data[$k]['itime'] = date ('Y-m-d H:i:s');
            }
            $pageNum = ceil ($count / $long);
            $result = [
                'page'    => $page,
                'pageNum' => $pageNum,
                'count'   => $count,
                'data'    => $data,
            ];
        }
        return $result;
    }

    /**
     * 编辑、添加
     * @param $params
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function post($params) {
        if (empty(trim ($params['title']))) {
            Err::add ('标题不能为空', RespCode::PARAMS_ERR);
            return false;
        }
        if (!empty($params['id'])) {
            if (!$old = self::where ('id', $params['id'])->select ()->toArray ()) {
                Err::add ('数据不存在', RespCode::PARAMS_ERR);
                return false;
            }
        }
        if (mb_strlen (trim ($params['title']), 'utf-8') > 30) {
            Err::add ('长度限制30', RespCode::PARAMS_ERR);
            return false;
        }
        $data = [
            'title'    => trim ($params['title']),
            'url'      => $params['url'],
            'showtime' => intval ($params['showtime']),
        ];
        if (empty($params['id'])) {
            $data['itime'] = time ();
            self::insert ($data);
        } else {
            //删除图片
            if ($old[0]['url'] != $params['url'] && !empty($old[0]['url'])) {
                $oss = Oss::getInstance ();
                $oss->delOneFile ($old[0]['url']);
            }
            self::where ('id', $params['id'])->update ($data);
        }
        return true;
    }

    /**
     * 删除
     * @param $params
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public static function del($params) {
        if (!$old = self::where ('id', $params['id'])->select ()->toArray ()) {
            Err::add ('数据不存在', RespCode::PARAMS_ERR);
            return false;
        }
        //删除图片
        if (self::where ('id', $params['id'])->delete ()) {
            $oss = Oss::getInstance ();
            $oss->delOneFile ($old[0]['url']);
        }
        return true;
    }

}
